package com.javaranger.mail;

import java.net.InetAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;


/**
 * 纯文本邮件服务类.
 */
public class SimpleMailService {
	private static Logger logger = LoggerFactory
			.getLogger(SimpleMailService.class);

	private JavaMailSender mailSender;
	private static ExecutorService executorService = Executors
			.newFixedThreadPool(5);
	private static String computeName = null;
	private String mails = "taoshengyijiu1986@163.com";
	
	private static ApplicationContext applicationContext = null;

	/**
	 * 发送纯文本的用户修改通知邮件.
	 */
	private void sendNotificationMail(final String title, final String content,
			final String[] emails) {
		SimpleMailMessage msg = new SimpleMailMessage();
		if (computeName == null) {
			try {
				computeName = InetAddress.getLocalHost().getHostName();
				computeName = computeName.replace(".qunar.com", "");
			} catch (Exception e) {
				computeName = "hs-alert";
			}
		}

		msg.setFrom(computeName);
		if (mails.indexOf(";") != -1) {
			String[] peoples = StringUtils.split(mails, ";");
			if (emails != null && emails.length > 0) {
				String[] temp = new String[peoples.length + emails.length];
				System.arraycopy(peoples, 0, temp, 0, peoples.length);
				System.arraycopy(emails, 0, temp, peoples.length, emails.length);
				peoples = temp;
			}
			msg.setTo(peoples);
		} else {
			msg.setTo(mails);
		}
		msg.setSubject(title);
		msg.setText(content);

		try {
			mailSender.send(msg);
			if (logger.isInfoEnabled()) {
				logger.info("纯文本邮件已发送至{}", StringUtils.join(msg.getTo(), ","));
			}
		} catch (Exception e) {
			logger.error("发送邮件失败", e);
		}
	}

	public void sendMail(final String title, final String content,
			final String[] emails) {
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				sendNotificationMail("", "", emails);
			}
		});
	}

	public void sendMail(final String title, final String content) {
		sendMail(title, content, null);
	}

	/**
	 * Spring的MailSender.
	 */
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public static void sendAlertMail(final String title, final String content) {
		SimpleMailService mailService = applicationContext
				.getBean(SimpleMailService.class);
		if (mailService != null) {
			mailService.sendMail(title, content, null);
		}
	}

	public static void sendAlertMail(final String title, final String content,
			final String[] emails) {
		SimpleMailService mailService = applicationContext
				.getBean(SimpleMailService.class);
		if (mailService != null) {
			mailService.sendMail(title, content, emails);
		}
	}
	
	public static void main(String[] args) {
		SimpleMailService.sendAlertMail("test", "test");
	}
	
}
